package iscte.ai.kcgtd;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CheckListAdapter extends BaseAdapter{

	private Activity activity;
	private Task[] buffer;
	private Button checkButton;
	private Button refuseButton;

	public CheckListAdapter(Activity activity, Task[] buffer) {
		this.activity = activity;
		this.buffer = buffer;
	}

	@Override
	public int getCount() {
		return buffer.length;
	}

	@Override
	public Object getItem(int position) {
		return buffer[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		
		LayoutInflater inflator = activity.getLayoutInflater();
		view = inflator.inflate(R.layout.check_list_item, null);
		TextView textView = (TextView) view.findViewById(R.id.check_list_item_SuggestionItemName);
		
		KCGTDTime now = new KCGTDTime();
		
		if(now.isSameDay(buffer[position].getTimeStart()))
			textView.setText(buffer[position].getName() + "\n" + "Hoje -> " + buffer[position].getTimeStart().seeHour() + " - " + buffer[position].getTimeEnd().seeHour());
		else
			textView.setText(buffer[position].getName() + "\n" + "Amanha -> " + buffer[position].getTimeStart().seeHour() + " - " + buffer[position].getTimeEnd().seeHour());
		
		checkButton = (Button) view.findViewById(R.id.check_list_item_SuggestionbuttonConfirm);
		refuseButton = (Button) view.findViewById(R.id.check_list_item_SuggestionbuttonRefuse);
		
		Boolean isConfirmed = false;
		
		checkButton.setOnClickListener(new ButtonListener(buffer[position], "v", textView, isConfirmed));
		refuseButton.setOnClickListener(new ButtonListener(buffer[position], "x", textView, isConfirmed));
		
		return view;
	}

	class ButtonListener implements OnClickListener {
		private Task task;
		private String type;
		private TextView textView;
		private Boolean isConfirmed;
		
		public ButtonListener(Task task, String type, TextView textView, Boolean isConfirmed) {
			this.task = task;
			this.type = type;
			this.textView = textView;
			this.isConfirmed = isConfirmed;
		}
		
		@Override
		public void onClick(View v) {
			if(type.equals("v")){
				task.setScheduled(true);
				checkButton.setEnabled(false);
				refuseButton.setEnabled(true);
				textView.setBackgroundColor(Color.rgb(76, 153, 0));
				Toast.makeText(v.getContext(), task.getName() + " - Confirmed", Toast.LENGTH_SHORT).show();
				//
			}else{
				task.setScheduled(false);
				refuseButton.setEnabled(false);
				checkButton.setEnabled(true);
				textView.setBackgroundColor(Color.rgb(153, 0, 0));
				Toast.makeText(v.getContext(), task.getName() + " - Rejected", Toast.LENGTH_SHORT).show();
				//diminuir a pontua��o do horarios
				//dimnuir a pontua��o do tipo de tarefa
			}
			
			if(!isConfirmed){
				Suggestion.suggestionsAccepted++;
				isConfirmed = true;
			}
			
		}
		
	}
	
}
